package com.tdzh.maimaiti.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tdzh.maimaiti.R;
import com.tdzh.maimaiti.biz.GetLocalRequest;

import java.util.List;
import java.util.Map;

/**
 * Created by hisou on 2016/5/10.
 */
public class HomeAdapter extends BaseAdapter {

    private SharedPreferences requestsSharedPreferences;
    private LayoutInflater mLayoutInflater;
    private List<Map<String, String>> dataList;

    public HomeAdapter(LayoutInflater layoutInflater, List<Map<String, String>> dataList){
        this.mLayoutInflater = layoutInflater;
        this.dataList = dataList;
       // requestsSharedPreferences = GetLocalRequest.getLocalRequest();
    }

    private class ViewHolder {
        public TextView title;
        public TextView brief;
        public TextView author;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = mLayoutInflater.inflate(R.layout.request_item, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.brief = (TextView) view.findViewById(R.id.brief);
            holder.author = (TextView) view.findViewById(R.id.requester);
            view.setTag(holder);        // 给View添加一个格外的数据
        } else {
            holder = (ViewHolder) view.getTag(); // 把数据取出来
        }

        /*interest = getSharedPreferences("interest", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = interest.edit();*/

        Map<String, String> map = dataList.get(position);
        holder.title.setText(map.get("title"));
        if(map.get("details").length() > 15){
        holder.brief.setText(map.get("details").substring(0, 15) + "...");}
        else {
            holder.brief.setText(map.get("details"));
        }
        holder.author.setText(map.get("author"));

        return view;
    }
}
